package com.sisyphus.spinach.extension.form;


/**
 * 效验工具
 */
public class FormValidation {

    public static FormValidation add(IFormRule... rules) {
        return new FormValidation(rules);
    }


    public interface Callback {

        void call(boolean isSuccessful, CharSequence errorMessage);

    }


    private final IFormRule[] rules;


    private FormValidation(IFormRule... rules) {
        this.rules = rules;
    }

    public void start(Callback callback) {
        for (IFormRule rule : rules) {
            if (!rule.validation()) {
                callback.call(false, rule.getErrorMessage());
                return;
            }
        }

        callback.call(true, null);
    }

}
